package com.erhuogo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.GridPageBean;
import com.erhuogo.bean.PageBean;
import com.erhuogo.dao.CityDao;
import com.erhuogo.dao.MemberDao;
import com.erhuogo.dao.ProvinceDao;
import com.erhuogo.dao.SchoolDao;
import com.erhuogo.entity.City;
import com.erhuogo.entity.CommodityInfo;
import com.erhuogo.entity.Favorite;
import com.erhuogo.entity.Member;
import com.erhuogo.entity.Province;
import com.erhuogo.entity.School;
import com.erhuogo.enumerate.OpenStatus;
import com.erhuogo.service.MemberService;

/**
 * 会员Service的实现类
 * @author aibinxiao
 * @date 2016年10月9日 下午4:19:33
 */
public class MemberServiceImpl implements MemberService { 
	// 日志
	private static Logger logger = LoggerFactory.getLogger(MemberServiceImpl.class);
	
	// 注入memberDao
	private MemberDao memberDao;
	private Member member=null;
	
	public MemberDao getMemberDao() {
		return memberDao;
	}

	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	
	// 注入provinceDao
	private ProvinceDao provinceDao;

	public ProvinceDao getProvinceDao() {
		return provinceDao;
	}

	public void setProvinceDao(ProvinceDao provinceDao) {
		this.provinceDao = provinceDao;
	}

	// 注入CityDao
	private CityDao cityDao;

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}
	
	// 注入SchoolDao
	private SchoolDao schoolDao;

	public SchoolDao getSchoolDao() {
		return schoolDao;
	}

	public void setSchoolDao(SchoolDao schoolDao) {
		this.schoolDao = schoolDao;
	}

	/**
	 * 1.会员登陆Service实现类
	 */
	@Override
	public Member login(String username, String password) {
		return memberDao.login(username, password);
	}

	/**
	 * 2.验证手机号是否已经注册Service实现类
	 * @return
	 */
	@Override
	public boolean findByPhoneNumber(String phoneNumber) {
		return memberDao.findByPhoneNumber(phoneNumber);
	}

	/**
	 * 3.用户注册Service实现类
	 */
	@Override
	public boolean addMember(Member member) {
		// 根据省份id获取到省份信息
		Province province = provinceDao.findProvinceById(Integer.valueOf(member.getProvince()));
		member.setProvince(province.getName());
		// 根据城市id获取到城市信息
		City city = cityDao.findCitysById(Integer.valueOf(member.getCity()));
		member.setCity(city.getName());
		// 根据高校id获取到高校信息
		School school = schoolDao.findSchoolById(Integer.valueOf(member.getSchool()));
		member.setSchool(school.getName());
		// 设置其他的默认信息
		member.setNickName("erhuo_" + member.getPhoneNumber());
		member.setPortrait("img/portrait/portrait.png");
		member.setSex(1);
		member.setShieldStatus(0);
		member.setRegTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		member.setAlterTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		return memberDao.addMember(member);
	}

	/**
	 * 4.分页查询用户Service实现类
	 */
	@Override
	public GridPageBean<Member> findByPage(String pageNo, String pageSize, String id, String nickName, String startDate,
			String endDate) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = memberDao.countByParams(id, nickName, startDate, endDate);
		GridPageBean<Member> result = new GridPageBean<Member>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Member> rows = memberDao.findPageByParams(id,nickName,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<Member> rows = memberDao.findPageByParams(id,nickName,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	/**
	 * 5.根据ID屏蔽用户Service实现类
	 * @throws Exception 
	 */
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			memberDao.updateStatus(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			memberDao.updateStatus(id, OpenStatus.SHIELD.getStatusCode());
		}
	}

	@Override
	public Member getMemberById(String id) {
		member=memberDao.getMemberById(id);
		return member;
	}

	@Override
	public boolean checkPassword(Member member) {
		boolean flag=memberDao.checkPassword(member);
		return flag;
	}

	@Override
	public boolean alterPws(String memId, String newPws) {
		boolean flag=memberDao.alterPws(memId, newPws);
		return flag;
	}

	@Override
	public boolean uploadPic(int memId, String path) {
		boolean flag=memberDao.uploadPic(memId, path);
		return flag;
	}

	@Override
	public PageBean getCollections(int memId,String num) {
		int pageNum = 1;//页码默认值是1
		if(num!=null){
			pageNum = Integer.parseInt(num);
		}
		int totalRecords = memberDao.getCollectionsNum(memId);
		PageBean<Favorite> pageBean = new PageBean(pageNum, totalRecords, 10);
		//查询分页后的具体记录
		if(totalRecords <= pageBean.getPageSize()){
			List<Favorite> records = memberDao.getCollections(memId, pageBean.getStartIndex(), totalRecords);
			pageBean.setRecords(records);
		} else{
			List<Favorite> records = memberDao.getCollections(memId, pageBean.getStartIndex(), pageBean.getPageSize());
			pageBean.setRecords(records);
		}
		return pageBean;
	}

	@Override
	public boolean deleteCollect(int fId) {
		// TODO Auto-generated method stub
		return memberDao.deleteCollect(fId);
	}
	
}
